package uk.co.penguinpower.yasl;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class YASL extends Activity { 
	//static Context contexts;
	public static DBManager db;
	Spinner cmbLoadList;
	static String listz[] = {};
	
    @Override
    public void onCreate(Bundle savedInstanceState) { //Called when the activity is first created.
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //create the DB and add a default store and list
        db = new DBManager(this);
        db.firstStore();
        db.firstList();
        
        final newListDialog dialog = new newListDialog(this);
        
        //get all shopping lists
        refreshListz(); //fixme:figure out how to refresh listzAdapter with this
        ArrayAdapter<String> listzAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item,listz);
        
        //Create new list button
        Button btnNewList = (Button) findViewById(R.id.btnNewList);
        btnNewList.setOnClickListener(new OnClickListener() {
        	@Override
    		public void onClick(View v) {
        		dialog.show();
    		}
        });
        
        //List Selector Combobox
        cmbLoadList = (Spinner) findViewById(R.id.cmbLoadList);
        cmbLoadList.setPrompt("Select a List");
        cmbLoadList.setAdapter(listzAdapter);
                
        //fixme: added this button as I can't figure out how to make the spinner work properly
        Button btnOpenList = (Button) findViewById(R.id.btnOpenList);
        btnOpenList.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String list = listz[cmbLoadList.getSelectedItemPosition()];
				
				//open the list activity
				Intent intent = new Intent();
				intent.setClassName("uk.co.penguinpower.yasl", "uk.co.penguinpower.yasl.ListViewer");
				intent.putExtra("uk.co.penguinpower.yasl.listName", list);
				startActivity(intent);				
			}
        	
        });
        
        //Store Editor button
        Button btnStoreEditor = (Button) findViewById(R.id.btnStoreEditor);
        btnStoreEditor.setOnClickListener(new OnClickListener() {
    		public void onClick(View v) {
    			//Open the Store Editor Activity
    		}
    	});
        
        //Tour button
        Button btnHOWTO = (Button) findViewById(R.id.btnHOWTO);
        btnHOWTO.setOnClickListener(new OnClickListener() {
    		public void onClick(View v) {
    			//Demo the apps functionality
    		}
    	});   
    }
    
    //refresh the list selector
    public static void refreshListz() {
    	listz = db.getAllLists().toArray(listz);
    	//listzAdapter.notifyDataSetChanged();
    }
}